//(function($) {
    $(document).ready(function() {
    	var whereClause={"all":"all"};
    	$.ajax({
    		url:'http://localhost:8080/easyskool/rest/masterservice/getMasterSubject',
    		type:'POST',
    		data: '{"user":"user","whereClause":'+null+'}',
//    		data: '{"user":"user","whereClause":"}',
    		dataType: 'json',
    		contentType: "application/json; charset=utf-8",
    		success:function (data,testStatus,response) {
//    
    			var json =JSON.parse(response.responseText);// $.parseJSON(response.responseText);
 
    			$.each(json, function(i, item) {
    			$("#mytable").append('<tr id = '+i+'><td class="subject_id">'+item.subjectId+'</td>'+
    					'<td class="subject_name">'+item.subjectName+'</td>'+
    					'<td class="subject_description">'+item.subjectDesc+'</td>'+
    					'<td class="created_by">'+item.createdBy+'</td>'+
    					'<td class="created_date">'+item.createdDate+'</td>'+
    					'<td class="modified_by">'+item.modifiedBy+'</td>'+
    					'<td class="modified_date">'+item.modifiedDate+'</td>'+
    					 "<td><input type = 'button' value = 'Update' class  = 'btn btn-info' id ='update' data-toggle = 'modal' data-target='#myModal' /></td>" +
    					 "<td><input type = 'button' value = 'Delete' class  = 'btn btn-danger' id ='remove'  /></td></tr>");
    		//	 "<td><input type = 'button' value = 'Update' class  = 'btn btn-info' id ='update' data-toggle = 'modal' data-target='#myModal' /></td>" +
			//	 "<td><input type = 'button' value = 'Delete' class  = 'btn btn-danger' id = '"+i+"'  /></td></tr>");
    			});
//    			"<td><input type = 'button' value = 'Delete' class  = 'btn btn-danger' id ='"+i+"'  /></td></tr>");
    			},
    		error:function(data,testStatus,jqXHR){
    			alert("Bad thing happend! " + jqXHR+data.responseText+" "+testStatus);
    		}
    	});
    });	
    
    $('#updateMasterSubject').validate({
        rules: {
            sectionId: {
                maxlength: 15,
                required: true
            },
            sectionName: {
                maxlength: 15,
                required: true
            },
            sectionDesc: {
                maxlength: 15,
                required: true
            },
            createdBy: {
                required: true
            },
            createdDate: {
                required: true,
                date: true
            },
            modifiedBy: {
                required: true
            },
            modifiedDate: {
                required: true,
                date: true
            }
            
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            if(element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }
	});
    
	$('#insertMasterSubjectform').validate({
        rules: {
            sectionId: {
                maxlength: 15,
                required: true
            },
            sectionName: {
                maxlength: 15,
                required: true
            },
            sectionDesc: {
                maxlength: 15,
                required: true
            },
            createdBy: {
                required: true
            },
            createdDate: {
                required: true,
                date: true
            },
            modifiedBy: {
                required: true
            },
            modifiedDate: {
                required: true,
                date: true
            }
            
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            if(element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }
    });
	
    $('.form-fields').on('click', '#remove', function(){
    	 var curRowId = $(this).closest('tr').attr("id");
    	 var map ="";
    	 map = map+ 	'{ "subjectId" 	: "' + $('#' + curRowId + ' td.subject_id').text() +'",';
    	 map = map	+ 	'"subjectName" 	: "' + $('#' + curRowId + ' td.subject_name').text() 			+'",';
         map = map	+ 	'"subjectDesc" 	: "' + $('#' + curRowId + ' td.subject_description').text() 	+'",';
         map = map 	+ 	'"createdBy" 	: "' + $('#' + curRowId + ' td.created_by').text() 			+'",';
         map = map 	+	'"createdDate" 	: "' + $('#' + curRowId + ' td.created_date').text() 		+'",';
         map = map	+ 	'"modifiedBy" 	: "' + $('#' + curRowId + ' td.modified_by').text() 		+'",' ;
         map = map 	+	'"modifiedDate"	: "' + $('#' + curRowId + ' td.modified_date').text() 		+'"}}';
 alert(map);
    	var row = $(this).closest('tr');
        row.remove();
        				$.ajax({
        					url : 'http://localhost:8080/easyskool/rest/masterservice/deleteMasterSubject',
    						type : 'POST',
    						data : '{"user":"aa","whereClause" :' +map,
    						dataType : 'json',
    						contentType : "application/json; charset=utf-8",
    						success : function(data,testStatus,jqXHR) {
    							alert(jqXHR.responseText);
    							window.location = "./easyskool/WebPages/Admin/AdminHome.jsp";
    						},
    						error : function(data,testStatus,jqXHR) {
    							alert("Error: ! "+ jqXHR+ data.responseText+ " "+ testStatus);
    						}
        					});
        
    });
    $('.form-fields').on('click', '#update', function(){
    	 var curRowId = $(this).closest('tr').attr("id");
         $('#usubjectId').val( $('#' + curRowId + ' td.subject_id').text() );
         $('#usubjectId').data('abc',$('#' + curRowId + ' td.subject_id').text());
         $('#usubjectName').val( $('#' + curRowId + ' td.subject_name').text() );
         $('#usubjectDesc').val( $('#' + curRowId + ' td.subject_description').text() );
         $('#ucreatedBy').val( $('#' + curRowId + ' td.created_by').text() );
         $('#ucreatedDate').val( $('#' + curRowId + ' td.created_date').text() );
         $('#umodifiedBy').val( $('#' + curRowId + ' td.modified_by').text() );
         $('#umodifiedDate').val( $('#' + curRowId + ' td.modified_date').text() );
         });
    
    $('#updateMasterSubject').on('click', '#update-db', function(){
    alert("jhj");	
    	hiddenData = ($('#subjectId').data('abc'));
  alert('j');
    			formData = ($('#updateMasterSubject').serializeArray());
    			 alert(formData);
    				$.ajax({
    					url : 'http://localhost:8080/easyskool/rest/masterservice/updateMasterSubject',
						type : 'POST',
						data : '{"user":"'+ $("#aaCode").val()+ '","whereClause" : {"subjectId" : "'+hiddenData+'"},"updateClause":'+ converter(formData)+ '}',
						dataType : 'json',
						contentType : "application/json; charset=utf-8",
						success : function(data,testStatus,jqXHR) {
							alert(  jqXHR+ data.responseText+ " "+ testStatus);
							window.location = "./easyskool/WebPages/Admin/AdminHome.jsp";
						},
						error : function(data,testStatus,jqXHR) {
							alert("Error: ! "+ jqXHR+ data.responseText+ " "+ testStatus);
						}
    					});
    });
    
    $("#insert-db").click(function(){
    	alert("FA");
    	alert("in " );
    	if($('#insertMasterSubjectform').valid()) {
//    		alert("before:"+)
		formData = ($('#insertMasterSubjectform').serializeArray());
//		formData = ($('#createMasterSubject').serializeArray());
		alert(formData+" form");
		$.ajax({
			url:'http://localhost:8080/easyskool/rest/masterservice/insertMasterSubject',
			type:'POST',
			data:'{"subjectName":"'+ $("#subjectName").val()+ '","fields":'+ converter(formData)+ '}',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success:function (data,testStatus,jqXHR) {
				alert(jqXHR.responseText);
				// 	alert(response.rowId);
			},
			error:function(data,testStatus,jqXHR){
				alert("Error! " + jqXHR+data.responseText+" "+testStatus);
			}
		});
 
    	}
	});
    
    
